/**
 * Copyright (c) 2008, Andrew Carter
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are 
 * permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer. Redistributions in binary form must reproduce 
 * the above copyright notice, this list of conditions and the following disclaimer in 
 * the documentation and/or other materials provided with the distribution. Neither the 
 * name of Andrew Carter nor the names of contributors may be used to endorse or promote 
 * products derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.acarter.scenemonitor.propertydescriptor.definitions.renderstate;

import com.acarter.propertytable.Property;
import com.acarter.propertytable.PropertySection;
import com.acarter.propertytable.PropertySectionState;
import com.acarter.propertytable.properyobject.EnumPropertyObject;
import com.acarter.propertytable.properyobject.FloatPropertyObject;
import com.acarter.propertytable.properyobject.EnumPropertyObject.I_EnumPropertyObjectListener;
import com.acarter.propertytable.properyobject.FloatPropertyObject.I_FloatPropertyObjectListener;
import com.acarter.scenemonitor.propertydescriptor.propertyobject.ColorRGBAPropertyObject;
import com.acarter.scenemonitor.propertydescriptor.propertyobject.ColorRGBAPropertyObject.I_ColorRGBAPropertyObjectListener;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.MaterialState.ColorMaterial;
import com.jme.scene.state.MaterialState.MaterialFace;

/**
 * @author Carter
 *
 */
public class MaterialStatePropertyPage extends RenderStatePropertyPage {
	
	/**
	 * 
	 */
	public MaterialStatePropertyPage() {
		
		PropertySection section = new PropertySection("Material State");
		section.addProperty(new Property("Color Material", new EnumPropertyObject<ColorMaterial>()));
		section.addProperty(new Property("Material Face", new EnumPropertyObject<MaterialFace>()));
		section.addProperty(new Property("Ambient", new ColorRGBAPropertyObject()));
		section.addProperty(new Property("Diffuse", new ColorRGBAPropertyObject()));
		section.addProperty(new Property("Specular", new ColorRGBAPropertyObject()));
		section.addProperty(new Property("Emissive", new ColorRGBAPropertyObject()));
		section.addProperty(new Property("Shininess", new FloatPropertyObject()));

		section.setState(PropertySectionState.EXPANDED);
		
		model.addPropertySection(0, section);
	}
	
	/**
	 * 
	 * @param table
	 * @param object
	 */
	@SuppressWarnings("unchecked")
	@Override
	protected void updateListeners(final RenderState renderstate) {
		
		super.updateListeners(renderstate);
		
		if(!(renderstate instanceof MaterialState))
			return;
		
		final MaterialState state = (MaterialState)renderstate;
		
		EnumPropertyObject<ColorMaterial> colorMaterial = (EnumPropertyObject<ColorMaterial>)model.getPropertySection("Material State").getProperty("Color Material").getPropertyObject();
		
		colorMaterial.SetListener(new I_EnumPropertyObjectListener<ColorMaterial>() {

			public ColorMaterial readValue() {
				
				return state.getColorMaterial();
			}

			public void saveValue(ColorMaterial value) {
				
				state.setColorMaterial(value);
			}
		});
		
		EnumPropertyObject<MaterialFace> materialFace = (EnumPropertyObject<MaterialFace>)model.getPropertySection("Material State").getProperty("Material Face").getPropertyObject();
		
		materialFace.SetListener(new I_EnumPropertyObjectListener<MaterialFace>() {

			public MaterialFace readValue() {
				
				return state.getMaterialFace();
			}

			public void saveValue(MaterialFace value) {
				
				state.setMaterialFace(value);
			}
		});
		
		ColorRGBAPropertyObject ambient = (ColorRGBAPropertyObject)model.getPropertySection("Material State").getProperty("Ambient").getPropertyObject();
		
		ambient.SetListener(new I_ColorRGBAPropertyObjectListener() {

			public ColorRGBA readValue() {
				
				return state.getAmbient();
			}

			public void saveValue(ColorRGBA value) {
				
				state.setAmbient(value);
			}
		});
		
		ColorRGBAPropertyObject diffuse = (ColorRGBAPropertyObject)model.getPropertySection("Material State").getProperty("Diffuse").getPropertyObject();
		
		diffuse.SetListener(new I_ColorRGBAPropertyObjectListener() {

			public ColorRGBA readValue() {
				
				return state.getDiffuse();
			}

			public void saveValue(ColorRGBA value) {
				
				state.setDiffuse(value);
			}
		});
		
		ColorRGBAPropertyObject specular = (ColorRGBAPropertyObject)model.getPropertySection("Material State").getProperty("Specular").getPropertyObject();
		
		specular.SetListener(new I_ColorRGBAPropertyObjectListener() {

			public ColorRGBA readValue() {
				
				return state.getSpecular();
			}

			public void saveValue(ColorRGBA value) {
				
				state.setSpecular(value);
			}
		});
		
		ColorRGBAPropertyObject emissive = (ColorRGBAPropertyObject)model.getPropertySection("Material State").getProperty("Emissive").getPropertyObject();
		
		emissive.SetListener(new I_ColorRGBAPropertyObjectListener() {

			public ColorRGBA readValue() {
				
				return state.getEmissive();
			}

			public void saveValue(ColorRGBA value) {
				
				state.setEmissive(value);
			}
		});
		
		FloatPropertyObject shininess = (FloatPropertyObject)model.getPropertySection("Material State").getProperty("Shininess").getPropertyObject();
		
		shininess.SetListener(new I_FloatPropertyObjectListener() {

			public float readValue() {
				
				return state.getShininess();
			}

			public void saveValue(float value) {
				
				state.setShininess(value);
			}
		});
	}

}
